// @ts-ignore
/* eslint-disable */
import {request} from 'umi';
import {QuestionBody, QuestionListItem} from './data';

export async function dataList(params: {
  // query
  /** 当前的页码 */
  current?: number;
  /** 页面的容量 */
  pageSize?: number;
  selectMode?: boolean;
}) {
  let url = `/v2/enterprise/train/question/plan/list`;
  if (params.selectMode) {
    url = `/v2/enterprise/train/question/list`;
  }
  return request<{
    data: QuestionListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>(url, {
    method: 'GET',
    params: {
      ...params,
      pageNum: params.current,
      orderByColumn: 'createTime',
      isAsc: 'desc',
    },
  });
}

export async function loadData(param: QuestionBody, isPlan: boolean): Promise<{ data: QuestionBody }> {
  if (isPlan) {
    return request(`/v2/enterprise/train/question/plan/${param.questionId}`, {method: 'GET'});
  } else {
    return request(`/v2/enterprise/train/question/${param.questionId}`, {method: 'GET'});
  }
}

export async function addData(param: QuestionBody): Promise<{ data: number }> {
  return request('/v2/enterprise/train/question', {method: 'POST', data: param});
}

export async function updateData(param: QuestionBody, isPlan: boolean): Promise<{ data: number }> {
  if (isPlan) {
    return request('/v2/enterprise/train/question/plan', {method: 'PUT', data: param});
  } else {
    return request('/v2/enterprise/train/question', {method: 'PUT', data: param});
  }
}

export async function removeData(data: number[], isPlan: boolean, options?: { [key: string]: any }) {
  let url = `/v2/enterprise/train/question/`;
  if (isPlan) {
    url = `/v2/enterprise/train/question/plan/`;
  }
  return request<Record<string, any>>(url + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function removeQuestionData(
  dataId: number,
  data: number[],
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`/v2/enterprise/train/question/${dataId}/` + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function loadSelectTag() {
  return request(`/v2/enterprise/train/question/tags`, {method: 'GET'});
}

export async function questionList(dataId: number) {
  return request(`/v2/enterprise/train/question/${dataId}/list`, {
    method: 'GET',
  });
}


export async function download(params: {
  selectMode?: boolean;
}) {
  let url = `/v2/enterprise/train/question/plan/export`;
  if (params.selectMode) {
    url = `/v2/enterprise/train/question/export`;
  }
  return request(url, {
    method: 'POST',
    responseType: 'blob',
    params: {
      ...params,
    },
  }).then((res) => {
    const objectURL = URL.createObjectURL(res);
    let btn = document.createElement('a');
    btn.download = '试题模板.xlsx'; //文件类型
    btn.href = objectURL;
    btn.click();
    URL.revokeObjectURL(objectURL);
    // @ts-ignore
    btn = null;
  });
}

//
// export async function download(params: {
//   selectMode?: boolean;
// }) {
//   request(`/v2/enterprise/train/courseware/question/template`, {
//     method: 'POST',
//     // 必须加responseType: 'blob',
//     responseType: 'blob',
//   }).then((res) => {
//     const objectURL = URL.createObjectURL(res);
//     let btn = document.createElement('a');
//     btn.download = '试题模板.xlsx'; //文件类型
//     btn.href = objectURL;
//     btn.click();
//     URL.revokeObjectURL(objectURL);
//     // @ts-ignore
//     btn = null;
//   });
// }
